package com.broada.apm.model.performance;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public abstract class AbstractPerformance extends HashMap<String, Object> {

    private static final String YYYY_MM_DD_T_HH_MM_SS_ZZ = "yyyy-MM-dd'T'HH:mm:ssZZ";
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    protected void putString(String key, String val) {
        put(key, val);
    }
    public final void putBoolean(String key, boolean b) {
        put(key,b);
    }
    public final void clear(String key) {
        remove(key);
    }
    public final String getString(String key) {
        return (String) get(key);
    }

    public final Date getDate(String key) {
        try {
            Object obj = get(key);
            if(obj instanceof Long) return new Date((Long)obj);
            if(obj instanceof Date) return (Date)obj;
            if(obj instanceof String) return (Date) new SimpleDateFormat(YYYY_MM_DD_T_HH_MM_SS_ZZ).parse((String)obj);
            return null;
        } catch (ParseException e) {
            return null;
        }
    }

    protected final void putDate(String key, Date val) {
        put(key, val);
    }

    public boolean getBool(String key) {
        return containsKey(key) ? (boolean) get(key) : false;
    }

    protected void putBool(String key, boolean val) {
        put(key, val);
    }

    protected void putInt(String key, int val) {
        put(key, val);
    }

    public int getInt(String key) {
        return ((Number) get(key)).intValue();
    }

    protected void putLong(String key, long val) {
        put(key, val);
    }

    public long getLong(String key) {
        return ((Number) get(key)).longValue();
    }

    protected void putDouble(String key, double val) {
        put(key, val);
    }

    public double getDouble(String key) {
        return ((Number) get(key)).doubleValue();
    }

}
